/**   
 * @Title: TestServiceImplementation.java 
 * @Package cn.edu.cdtu.healthserver.service.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.edu.cdtu.healthserver.dao.TestDao;
import cn.edu.cdtu.healthserver.dao.impl.TestDaoImplementation;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.domain.TestType;
import cn.edu.cdtu.healthserver.service.TestService;

/** 
 * @ClassName: TestServiceImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class TestServiceImplementation implements TestService {
	
	private TestDao testDao = new TestDaoImplementation();

	/*
	 * (non-Javadoc)
	 * <p>Title: getTests</p> 
	 * <p>Description: </p> 
	 * @param testType
	 * @param listAvailable
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestService#getTests(cn.edu.scu.healthserver.domain.TestType, boolean)
	 */
	@Override
	public List<Test> getTests(TestType testType, boolean listAvailable) {
		// TODO Auto-generated method stub
		List<Test> tests = testDao.query(testType);
		if(listAvailable){
			return tests;
		}
		List<Test> removeList = new ArrayList<Test>();
		for(Test test:tests){
			if(0==test.getAvailable()){
				removeList.add(test);
			}
		}
		for (Test test : removeList) {
			tests.remove(test);
		}
		return tests;
	}

	/* (non-Javadoc)
	 * <p>Title: addTest</p> 
	 * <p>Description: </p> 
	 * @param testType
	 * @param test
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestService#addTest(cn.edu.scu.healthserver.domain.TestType, cn.edu.scu.healthserver.domain.Test)
	 */
	@Override
	public String addTest(TestType testType, Test test) {
		// TODO Auto-generated method stub
		String resultString = null;
		test.setType_id(testType.getId());
		if(testDao.add(test)){
			resultString = "添加测试成功！";
		}else{
			resultString = "添加测试失败！";
		}
		return resultString;
	}

	/* (non-Javadoc)
	 * <p>Title: update</p> 
	 * <p>Description: </p> 
	 * @param test
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestService#update(cn.edu.scu.healthserver.domain.Test)
	 */
	@Override
	public String update(Test test) {
		// TODO Auto-generated method stub
		String resultString = null;
		if(testDao.update(test)){
			resultString = "更新测试成功！";
		}else {
			resultString = "更新测试失败！";
		}
		return resultString;
	}

	/* (non-Javadoc)
	 * <p>Title: setAvailable</p> 
	 * <p>Description: </p> 
	 * @param test
	 * @param available
	 * @return 
	 * @see cn.edu.scu.healthserver.service.TestService#setAvailable(cn.edu.scu.healthserver.domain.Test, boolean)
	 */
	@Override
	public String setAvailable(Test test, boolean available) {
		// TODO Auto-generated method stub
		String resultString = null;
		if(available){
			test.setAvailable(1);
			if(testDao.update(test)){
				resultString = "设置测试有效成功！";
			}else {
				resultString = "设置测试有效失败！";
			}
		}else {
			test.setAvailable(0);
			if(testDao.update(test)){
				resultString = "设置测试失效成功！";
			}else {
				resultString = "设置测试失效失败！";
			}
		}
		return resultString;
	}

}
